﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bing_Wallpaper_Downloader
{
    class ReportError
    {
        public static void HandleError(Exception ex)
        {
            Form form = new Form();
            form.StartPosition = FormStartPosition.CenterScreen;
            form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            form.Height = 300;
            form.Width = 300;
            form.MinimumSize = new Size(form.Width, form.Height);
            form.MaximumSize = new Size(form.Width, form.Height);
            form.Text = string.Format("{0} Error", Application.ProductName);
            form.ShowInTaskbar = false;

            TableLayoutPanel tablelayoutpanel = new TableLayoutPanel();
            tablelayoutpanel.Dock = DockStyle.Fill;
            tablelayoutpanel.ColumnCount = 2;
            tablelayoutpanel.RowCount = 3;

            TableLayoutPanelCellPosition tablelayoutpanelcellposition;

            Label label = new Label();
            label.Text = "An error has occurred. The details of the error are below. To send the author an error report click the Send button; otherwise click the Cancel button.";
            label.Dock = DockStyle.Fill;
            tablelayoutpanel.SetColumnSpan(label, 2);
            tablelayoutpanelcellposition = new TableLayoutPanelCellPosition(0, 0);
            tablelayoutpanel.SetCellPosition(label, tablelayoutpanelcellposition);
            tablelayoutpanel.Controls.Add(label);

            TextBox textbox = new TextBox();
            textbox.Text = string.Format("Application: {1}{0}Version: {2}{0}{0}{3}", Environment.NewLine, Application.ProductName, Application.ProductVersion, ex);
            textbox.ReadOnly = true;
            textbox.Dock = DockStyle.Fill;
            textbox.Multiline = true;
            tablelayoutpanel.SetColumnSpan(textbox, 2);
            tablelayoutpanelcellposition = new TableLayoutPanelCellPosition(0, 1);
            tablelayoutpanel.SetCellPosition(textbox, tablelayoutpanelcellposition);
            tablelayoutpanel.Controls.Add(textbox);

            Button button1 = new Button();
            button1.Click += new EventHandler(button1_Click);
            button1.Text = "Send";
            button1.Anchor = AnchorStyles.Left;
            tablelayoutpanelcellposition = new TableLayoutPanelCellPosition(0, 2);
            tablelayoutpanel.SetCellPosition(button1, tablelayoutpanelcellposition);
            tablelayoutpanel.Controls.Add(button1);

            Button button2 = new Button();
            button2.Click += new EventHandler(button2_Click);
            button2.Text = "Cancel";
            button2.Anchor = AnchorStyles.Right;
            tablelayoutpanelcellposition = new TableLayoutPanelCellPosition(1, 2);
            tablelayoutpanel.SetCellPosition(button2, tablelayoutpanelcellposition);
            tablelayoutpanel.Controls.Add(button2);

            Button button3 = new Button();
            button3.Click += new EventHandler(button3_Click);
            button3.Text = "Restart Application";
            button3.Anchor = AnchorStyles.Right;
            tablelayoutpanelcellposition = new TableLayoutPanelCellPosition(0, 3);
            tablelayoutpanel.SetCellPosition(button3, tablelayoutpanelcellposition);
            tablelayoutpanel.Controls.Add(button3);

            tablelayoutpanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            tablelayoutpanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            tablelayoutpanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

            tablelayoutpanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 40));
            tablelayoutpanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
            tablelayoutpanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));

            form.Controls.Add(tablelayoutpanel);

            form.ShowDialog();
        }

        private static void button1_Click(object sender, EventArgs e)
        {
            string exception = ((Button)sender).FindForm().Controls[0].Controls[1].Text;

            //send report somehow...

            ((Button)sender).FindForm().Close();

            ((Button)sender).FindForm().Dispose();

            MessageBox.Show("Report Sent!");
        }

        private static void button2_Click(object sender, EventArgs e)
        {
            ((Button)sender).FindForm().Close();

            ((Button)sender).FindForm().Dispose();
        }

        private static void button3_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
    }
}
